package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Role;
import com.qk.management.mapper.RoleMapper;
import com.qk.management.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 李智勇
 * @data 2025/10/4 16:34
 */
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> list() {
        return roleMapper.selectAll();
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteById(id);
    }
    @Override
    public void update(Role role) {
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.update(role);
    }

    @Override
    public Role getById(Integer id) {
        Role role = roleMapper.selectById(id);
        return role;
    }

    @Override
    public PageResult<Role> page(String name, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Role> roles = roleMapper.selectBypage(name);
        Page<Role> pageInfo = (Page<Role>) roles;
        return PageResult.<Role>builder()
                .total(pageInfo.getTotal())
                .rows(pageInfo.getResult())
                .build();
    }

    @Override
    public void addRole(Role role) {
        role.setCreateTime(LocalDateTime.now());
        role.setUpdateTime(LocalDateTime.now());
        roleMapper.insert(role);
    }
}